package cn.uexpo.lhz.controller.admin;

import cn.uexpo.lhz.biz.ImportLogBiz;
import cn.uexpo.lhz.common.oldBase.BaseController;
import cn.uexpo.lhz.common.oldUtils.PageRequest;
import cn.uexpo.lhz.common.oldUtils.PageResult;
import cn.uexpo.lhz.core.http.HttpResult;
import cn.uexpo.lhz.domain.ImportLog;
import cn.uexpo.lhz.domain.ImportLogExample;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @ClassName:  ImportLogController
 * @Description:TODO(导入人员信息的日志)
 * @author: 李冬建
 * @date:   2020年6月4日 上午10:27:32
 * @Copyright:  1.0.0
 */
@RestController
@RequestMapping("/cert/importStaffLog")
public class ImportStaffLogController extends BaseController<ImportLog>{

	@Autowired
	private ImportLogBiz importLogBiz;



	@PostMapping(value="/findPage")
	public HttpResult findPage(@RequestBody PageRequest pageRequest) {
		PageResult result=findPage1(pageRequest);
		return HttpResult.ok(result);
	}


	private PageResult findPage1(@RequestBody PageRequest pageRequest) {

		Object keyword1 = pageRequest.getParamValue("keyword1");//关键字1:人员编号
		Object keyword2 = pageRequest.getParamValue("keyword2");//关键字2:姓名
//		Object keyword3 = pageRequest.getParamValue("keyword3");//关键字3:手机号
		Object fileName = pageRequest.getParamValue("fileName");//文件名


		Object keyword4 = pageRequest.getParamValue("keyword4");//关键字4:所属项目
		Object code = pageRequest.getParamValue("code");//关键字4:返回码
		ImportLogExample example=new ImportLogExample();
		String orderBy=" create_time ";//默认按照时间降序
		String sort=" desc ";//默认按照时间降序
		example.setOrderByClause(orderBy+sort);
		ImportLogExample.Criteria citeria=example.createCriteria();
		if (StringUtils.isNotBlank((String)keyword1)) {
			citeria.andKeyword1Like("%"+(String)keyword1+"%");
		}
		if (StringUtils.isNotBlank((String)keyword2)) {
			citeria.andKeyword2Like("%"+(String)keyword2+"%");
		}
//		if (StringUtils.isNotBlank((String)keyword3)) {
//			citeria.andKeyword3Like("%"+(String)keyword3+"%");
//		}
		if (StringUtils.isNotBlank((String)fileName)) {
			citeria.andFilenameLike("%"+(String)fileName+"%");
		}
		if (StringUtils.isNotBlank((String)keyword4)) {
			citeria.andKeyword4Like("%"+(String)keyword4+"%");
		}
		if (StringUtils.isNotBlank((String)code)) {
			try{
				int codeInt=Integer.parseInt((String)code);
				citeria.andCodeEqualTo(codeInt);
			}catch (Exception e) {
				e.printStackTrace();
				citeria.andCodeEqualTo(999);
        	}
		}

		PageResult result=importLogBiz.findPage(pageRequest,example);
		return result;
	}



	/**
	 * 项目类型
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/initProjectType", method = RequestMethod.POST)
	public HttpResult proTypes() throws Exception {
		List<JSONObject> list = new ArrayList<JSONObject>();
		List<String> items = importLogBiz.selectDistinctProTypes();
		JSONObject objInit = new JSONObject();
		objInit.put("id", "");
		objInit.put("text", "请选择");
		list.add(objInit);
		if(items!=null&&items.size()>0) {
			for (String item : items) {
				JSONObject obj = new JSONObject();
				obj.put("id", item);
				obj.put("text", item);
				list.add(obj);
			}

		}

		return HttpResult.ok(list);
	}
	/**
	 * 文件名
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/initFileNames", method = RequestMethod.POST)
	public HttpResult fileNames() throws Exception {
		List<JSONObject> list = new ArrayList<JSONObject>();
		List<String> items = importLogBiz.selectDistinctFileNames();
		JSONObject objInit = new JSONObject();
		objInit.put("id", "");
		objInit.put("text", "请选择");
		list.add(objInit);
		if(items!=null&&items.size()>0) {
			for (String item : items) {
				JSONObject obj = new JSONObject();
				obj.put("id", item);
				obj.put("text", item);
				list.add(obj);
			}

		}

		return HttpResult.ok(list);
	}

	/**
	 * 返回码
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/initCodes", method = RequestMethod.POST)
	public HttpResult codes() throws Exception {
		List<JSONObject> list = new ArrayList<JSONObject>();
		List<String> items = importLogBiz.selectDistinctCodes();
		JSONObject objInit = new JSONObject();
		objInit.put("id", "");
		objInit.put("text", "请选择");
		list.add(objInit);
		if(items!=null&&items.size()>0) {
			for (String item : items) {
				JSONObject obj = new JSONObject();
				obj.put("id", item);
				obj.put("text", item);
				list.add(obj);
			}
		}
		return HttpResult.ok(list);
	}


}
